#pragma once
#include "GoogleProtocol.h"
#include "GoogleUserData.h"
#include "..\..\..\..\..\ThirdParty\tinyxml-2.5.3\tinyxml.h"

namespace GoogleProtocol
{

struct tCContactAuth
{
	string id;
	string updated;
	string title;
	string name;
	string email;
	int totalResults; // total user acount number
};

struct tContactEntry
{
	// Because the stream from google reveiced code page is utf8
	//wstring id; // unique
	//wstring updated;
	//wstring title;
	//wstring address;

	string id;
	string updated;
	string address;
	// !!! need reparse...

	// Google User Information  
	string title;
	string given_name;
	string family_name;
	string full_name;
	string mobile_phoneNumber;
	string content;
	string work_email;
	string home_email;
	string work_phoneNumber;
	string home_phoneNumber;
	string home_address;
	string city;
	string street;
	string region;
	string post_code;
	string country;
	string postalAddress;
};

class CGoogleContact : public CGoogleProtocol
{
public:
	CGoogleContact();
	~CGoogleContact(void);

public:
	class GoogleContact
	{
	public:
		enum eEntryType { auther, entry };
		bool IsContactAuther() { return m_entryType == auther; }

	protected:
		eEntryType m_entryType;
	};

	class ContactAuther : public GoogleContact
	{
	public:
		ContactAuther() { m_entryType = auther; }
		operator tCContactAuth()
		{
			return data;
		}

	public:
		tCContactAuth data;
	};

	class ContactEntry : public GoogleContact
	{
	public:
		ContactEntry() { m_entryType = entry; }
		operator tContactEntry()
		{
			return data;
		}

	public:
		tContactEntry data;
	};

protected:
	char* UnicodeToGB2312(const wchar_t* szUnicodeString, int nLen);
	wchar_t * GB2312ToUnicode(const char * szGBString, int nLen);
	bool GetTextElement(TiXmlNode * pParent , wstring& strText);
	bool GetIntElement(TiXmlNode * pParent , int& n);
	wstring Utf8ToUnicode(const char* lpszA);

	void GetUnicode(char* pSrc,const char* pDst);
	void GetUnicodeString(string& str,const char* pDst);

//	BOOL Logon();
	BOOL InitUserData(void);
	BOOL ParseAllContact(CHttpBuffer* pStream, list<GoogleContact *>& lstContact);

public:
	// get all
	G_RET GetAllContact(list<GoogleContact *>& lstContact);

	// add
	G_RET CreateContact(const tContactEntry& contactEntry);

	// update
	G_RET UpdateContact(const tContactEntry& contactEntry);

	// delete
	G_RET DeleteContact(const tContactEntry& contactEntry);

	// get single
	G_RET GetSingleContact(tContactEntry& contactEntry, const char* pUniqueId);

public:
//	wstring GetErrorString(void);
//	wstring GetSysErrorMessage(DWORD dwLastErrCode);
	void SetUserData(CGoogleUserData* pUserData);
	BOOL GetAllCContactEntry(list<tContactEntry *>& lstContactEntry);
	BOOL BeginDiscovery(void);

private:
	list<GoogleContact *> m_lstAllContact;
	CGoogleUserData* m_pUserData;
	LRESULT m_lResult;
};

}